/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.explore;

import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.constant.Common;
import com.workcell.workdata.im.entity.MicroServerMenu;
import com.workcell.workdata.im.entity.Subscriber;
import com.workcell.workdata.im.model.response.PaginationEntity;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.MicroServerVO;
import com.workcell.workdata.im.service.MicroServerMenuService;
import com.workcell.workdata.im.service.MicroServerService;
import com.workcell.workdata.im.service.SubscriberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/micro/server")
@Api(produces = "application/json", tags = "公众号相关接口" )
public class MicroServerController {

	@Resource
	private MicroServerService microServerService;

	@Resource
	private SubscriberService subscriberService;

	@Resource
	private MicroServerMenuService microServerMenuService;

	@ApiOperation(httpMethod = "GET", value = "获取公众号详情")
	@ApiImplicitParam(name = "id", value = "公众号ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@GetMapping(value = "/{id}")
	public ResponseEntity<MicroServerVO> get(@PathVariable Long id) {
		return ResponseEntity.ok(microServerService.findOne(id));
	}


	@ApiOperation(httpMethod = "GET", value = "搜索公众号")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "index", value = "当前页最大ID", paramType = "path", dataTypeClass = Integer.class,example = "0"),
			@ApiImplicitParam(name = "currentPage", value = "当前页数(初始0)", paramType = "path",  dataTypeClass = Integer.class,example = "0"),
			@ApiImplicitParam(name = "name", value = "关键词", paramType = "query", dataTypeClass = String.class),
	})
	@GetMapping(value = "/search/{index}/{currentPage}")
	public PaginationEntity<MicroServerVO> search(@PathVariable long index,
                                                  @PathVariable int currentPage,
                                                  @RequestParam(required = false) String name) {
		Pageable pageable = PageRequest.of(currentPage, Common.API_PAGE_SIZE);

		Page<MicroServerVO> page = microServerService.search(index,name,pageable);

		return PaginationEntity.make(page);
	}

	@ApiOperation(httpMethod = "POST", value = "订阅公众号")
	@ApiImplicitParam(name = "id", value = "公众号ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@PostMapping(value = "/subscribe/{id}")
	public ResponseEntity<Void> subscribe(@PathVariable Long id, @ApiIgnore @UID Long uid) {

		Subscriber subscriber = new Subscriber();
		subscriber.setServerId(id);
		subscriber.setUid(uid);
		subscriberService.add(subscriber);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "取消订阅公众号")
	@ApiImplicitParam(name = "id", value = "公众号ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@DeleteMapping(value = "/subscribe/{id}")
	public ResponseEntity<Void> unsubscribe(@PathVariable Long id, @ApiIgnore @UID Long uid) {

		Subscriber subscriber = new Subscriber();
		subscriber.setServerId(id);
		subscriber.setUid(uid);
		subscriberService.delete(subscriber);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "GET", value = "获取公众号菜单列表")
	@ApiImplicitParam(name = "id", value = "公众号ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@GetMapping(value = "/menu/list/{id}")
	public ResponseEntity<List<MicroServerMenu>> list(@PathVariable Long id) {
		return ResponseEntity.ok(microServerMenuService.queryList(id));
	}
}
